﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using Jungle.Code.Core;
using System.Text;
using System.Security.Cryptography;
using Jungle.Users;


    public partial class loginbar : System.Web.UI.UserControl
    {       

        SessionKeeper thisSession = new SessionKeeper();
        protected override void OnInit(EventArgs e)
        {
            btn_Login.PostBackUrl = Request.RawUrl;
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            thisSession.SessionId = Session.SessionID.ToString();
            thisSession.GetData();

            if (thisSession.UserId == -1)
            {
                LoginState.Visible = true;
                LoginState.ActiveViewIndex = 0;

                HttpCookie cookie = Request.Cookies["RM3ffd3d41ccca0b1b9b2a8923b0b48b3c"];
                if (cookie != null)
                {
                    if (cookie["rm"] == "true")
                    {
                        chb_Remember.Checked = true;

                        thisSession.SessionId = Session.SessionID.ToString();
                        thisSession.UserId = Convert.ToInt32(cookie["ui"]);
                        thisSession.UserLogin = cookie["nm"];
                        thisSession.AdminLogin = "none";

                        thisSession.Create();

                        Response.Redirect(UrlHelper.GetSiteUrl());

                    }
                }
            }
            else 
            {
                LoginState.Visible = true;
                LoginState.ActiveViewIndex = 1;
                lbl_Greatings.Text = "Приветствуем, " + thisSession.UserLogin.ToString().Trim();
            }
        }
        protected void Logout(object sender, EventArgs e)
        {
            thisSession.DeleteSession(Session.SessionID.ToString());

            HttpCookie cookie = Response.Cookies["RM3ffd3d41ccca0b1b9b2a8923b0b48b3c"];
            cookie["rm"] = "false";

            Response.Redirect(UrlHelper.GetSiteUrl());
        }
        protected void Login(object sender, EventArgs e)
        {
            UserManager um = new UserManager();
            um.Email = txt_Login.Text;
            um.Password = UserManager.getMd5Hash(txt_Pass.Text.Trim());

            if (um.Login())
            {
                thisSession.SessionId = Session.SessionID.ToString();
                thisSession.UserId = um.UserId;
                thisSession.UserLogin = um.Email;
                thisSession.AdminLogin = "none";

                thisSession.Create();

                
                if (chb_Remember.Checked)
                {
                    HttpCookie cookie = Response.Cookies["RM3ffd3d41ccca0b1b9b2a8923b0b48b3c"];
                    cookie["rm"] = "true";
                    cookie["nm"] = um.Email;
                    cookie["ui"] = um.UserId.ToString();
                    cookie.Expires = DateTime.Now.AddDays(14);
                }
                
                Response.Redirect(UrlHelper.GetSiteUrl());
            }
            else
            {
                
                LoginState.ActiveViewIndex = 2;
                lbl_err.Visible = true;
                lbl_err.Text = "[Неправильный логин или пароль]";
            }
        }



    }
